<template>


  <div>
    <!--    搜索区域-->
    <div style="margin: 10px 0">
      <el-button type="primary" @click="add">新增</el-button>
      <el-input v-model="search" placeholder="请输入关键字" style="width: 20%;" clearable></el-input>
      <el-button type="primary" style="margin-left: 5px" @click="load">查询</el-button>
    </div>

    <el-table
        :data="tableData"
        style="width: 100%">
      <el-table-column
          prop="id"
          label="ID"
          width="180">
      </el-table-column>
      <el-table-column
          prop="field"
          label="名称"
          width="180">
      </el-table-column>
      <el-table-column
          prop="fieldValue"
          label="限制">
      </el-table-column>
      <el-table-column label="操作">
        <template #default="scope">
          <el-button
              size="mini" type="primary"
              @click="handleEdit(scope.row)">编辑
          </el-button>
          <el-popconfirm title="确定删除吗？" @confirm="handleDelete(scope.row.id)">
            <template #reference>
              <el-button size="mini" type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div style="margin: 10px 0">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[5, 10, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
      <el-dialog title="提示" v-model="dialogVisible" width="30%" @confirm="save" >
        <el-form>
          <el-form-item label="名称">
            <el-input v-model="form.field" placeholder="请输入名称"></el-input>
          </el-form-item>
          <el-form-item label="限制">
            <el-input v-model="form.fieldValue" placeholder="请输入限制"></el-input>
          </el-form-item>
        </el-form>
        <template #footer>
          <span class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="save">确 定</el-button>
          </span>
        </template>
      </el-dialog>
    </div>
  </div>


</template>


<script>

import request from "@/utils/request";
import {nextTick} from "vue";

export default {
  name: "limitTable",
  data() {
    return {
      tableData: [],
      dialogVisible: false,
      user: {},
      form: {},
      search: '',
      idList: [],
      currentPage: 1,
      pageSize: 10,
      total: 0,
    }
  },
  mounted() {
    let userStr = sessionStorage.getItem("user") || "{}"
    this.user = JSON.parse(userStr)
    this.load();
  },
  methods: {
    load() {
      request.get("/limitTable",
          {
            params: {
              current: this.currentPage,
              size: this.pageSize,
              field: this.search
            }
          }).then(res => {
        console.log(res)
        if (res.code === '0') {
          this.tableData = res.data.records;
          this.total = res.data.total
        } else {
          this.$message({
            type: "error",
            message: res.msg
          })
        }
      })
    },
    handleEdit(row) {
      this.form = JSON.parse(JSON.stringify(row));
      this.dialogVisible = true;
    },
    handleDelete(id) {
      this.idList.push(id);
      request.delete("/limitTable", {params: {idList: this.idList.join(",")}}).then(res => {
        if (res.code === '0') {
          this.$message({
            type: "success",
            message: "删除成功"
          })
          this.load();
        } else {
          this.$message({
            type: "error",
            message: res.msg
          })
        }
      })
          , nextTick(() => {
        this.idList = [];
      })
    },
    add() {
      this.form = {};
      this.dialogVisible = true;
    },
    save() {
      if (this.form.id) {  // 更新
        request.put("/limitTable", this.form).then(res => {
          console.log(res)
          if (res.code === '0') {
            this.$message({
              type: "success",
              message: "更新成功"
            })
          } else {
            this.$message({
              type: "error",
              message: res.msg
            })
          }
          this.dialogVisible = false;
          this.load();
        })
      } else {
        request.post("/limitTable", this.form).then(res => {
          console.log(res)
          if (res.code === '0') {
            this.$message({
              type: "success",
              message: "新增成功"
            })
          } else {
            this.$message({
              type: "error",
              message: res.msg
            })
          }
          this.dialogVisible = false;
          this.load();
        })
      }
    },
    handleSizeChange(pageSize) {   // 改变当前每页的个数触发
      this.pageSize = pageSize
      this.load()
    },
    handleCurrentChange(pageNum) {  // 改变当前页码触发
      this.currentPage = pageNum
      this.load()
    },
    handleSearch() {
      this.currentPage = 1;
      this.load();
    }
  }
}
</script>

<style scoped>

</style>